package com.ifnk.portaljava.dapWeb.common;

import com.ifnk.portaljava.dapWeb.pojo.User;
import flex.messaging.FlexContext;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.UUID;

public class Util {
	private Log logger = LogFactory.getLog(Util.class);

	public String getUuid() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		String uuid = s.substring(0, 8) + s.substring(9, 13)
				+ s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
		return uuid;
	}

	public User getUserSession() {
		User user = (User) FlexContext.getHttpRequest().getSession()
				.getAttribute("user");
		return user;
	}

	public void userQuit() {
		FlexContext.getHttpRequest().getSession().removeAttribute("user");
		FlexContext.getHttpRequest().getSession().invalidate();
		logger.info("用户退出");
	}

	public static void routeMessage(MessageBroker messageBroker,
			String subTopic, String clientId, Object obj) {
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("callTraceChannel");
		msg.setHeader("DSSubtopic", subTopic);
		msg.setClientId(clientId);
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setTimestamp(System.currentTimeMillis());
		msg.setBody(obj);
		messageBroker.routeMessageToService(msg, null);
	}
	
	public static void fileSort(File[] files) {
		File temp;
		for(int i=0; i< files.length; i++) {
			for(int j=files.length - 1; j > i; j--) {
				if(Integer.parseInt(files[i].getName().replace(".dat", "")) > Integer.parseInt(files[j].getName().replace(".dat", ""))) {
					temp = files[i];
					files[i] = files[j];
					files[j] = temp;
				}
			}
		}
	}
}
